<?php

/**
 * Unlimited v1.1
 */

class Unlimited{

	static public function unlimitedForLevel($data,$html='┗━',$parent_name='pid' , $pid_name ='id',$pid=0,$level=0){
		$arr=array();
		foreach ($data as $key => $value) {
			if ($value[$parent_name]==$pid) {
				$value['level']=$level+1;
				$value['html']=str_repeat($html, $level);
				$arr[]=$value;
				$arr=array_merge($arr,self::unlimitedForLevel($data,$html,$parent_name,$pid_name,$value[$pid_name],$level+1));
			}
		}
		return $arr;
	}

	static public function unlimitedForNav($data,$name='item',$id_name = 'id',$parent_name='pid',$pid=0){
		$arr=array();
		foreach ($data as $key => $value) {
			if ($value[$parent_name]==$pid) {
				$value[$name]=self::unlimitedForNav($data,$name,$id_name,$parent_name,$value[$id_name]);
				$arr[]=$value;
			}
		}
		return $arr;
	}

	static public function getParents($data,$id ,$id_name='id',$parent_name='pid'){
		$arr=array();
		foreach ($data as $key => $value) {
			if ($value[$id_name]==$id) {
				$arr[]=$value;
				$arr=array_merge(self::getParents($data,(int)$value[$id_name]),$arr);
			}
		}
		return $arr;
	}

	static public function getChild($data,$id,$id_name='id',$parent_name='pid'){
		$arr = array();
		foreach ($data as $key => $value) {
			if($value[$parent_name] == $id){
				$arr[] = $value;
				$arr = array_merge(self::getChild($data,(int)$value[$id_name],$id_name,$parent_name),$arr);
			}
		}
		return $arr;
	}
}